package com.xinghou.timer.common;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ApplicationContext {
	private static final ApplicationContext context=new ApplicationContext();
	private ClassPathXmlApplicationContext xmlApplicationContext;
	

	public ClassPathXmlApplicationContext get() {
		return xmlApplicationContext;
	}


	private ApplicationContext(){
		System.out.println("init spring ClassPathXmlApplicationContext");
		ClassPathXmlApplicationContext xmlApplicationContext = new ClassPathXmlApplicationContext("classpath:/spring/applicationContext-dubbo.xml");
		this.xmlApplicationContext = xmlApplicationContext;
		 System.out.println("init spring ClassPathXmlApplicationContext success");
		
	}
	
	public static ApplicationContext getContext(){
		return context;
	}
	public <T> T getBean(String name) {
		@SuppressWarnings("unchecked")
		T t = (T) xmlApplicationContext.getBean(name);
		return t;
	}

	public  <T> T getBean(Class<T> name) {
		T t = xmlApplicationContext.getBean(name);
		return t;
	}

	public static  void exit() {
		if(context!=null){
			context.close();
		}
	}

	private void close() {
		xmlApplicationContext.close();
	}
	
	

}
